package com.mrogrape.notepad.ui.find;

import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;

import com.mrogrape.notepad.ui.HistoryTextField;

public class AssistantButtonMenu implements ActionListener {

	public AssistantButtonMenu(List<String> historyList,
			HistoryTextField textField) {

		this.historyList = historyList;
		this.textField = textField;
		
		initializeMenu();
	}
	
	@Override
	public void actionPerformed(ActionEvent e) {

		Object src = e.getSource();
		
		if(src == this.tabMenuItem){
			
			this.textField.setText("\t");
		}
		else if(src == this.clearHistoryMenuItem){
			
			this.historyList.clear();
			this.textField.setHistory(this.historyList);
		}
		else{
			
			Component button = (Component) src;
			this.menu.show(button, 0, button.getHeight());
		}
	}
	
	private void initializeMenu(){
		
		this.menu = new JPopupMenu();
		this.tabMenuItem = new JMenuItem("Tab Character");
		this.clearHistoryMenuItem = new JMenuItem("Clear All The Histories");
		
		this.menu.add(this.tabMenuItem);
		this.menu.add(this.clearHistoryMenuItem);

		this.tabMenuItem.addActionListener(this);
		this.clearHistoryMenuItem.addActionListener(this);
	}
	
	private List<String> historyList;
	private HistoryTextField textField;
	
	private JPopupMenu menu;
	private JMenuItem tabMenuItem;
	private JMenuItem clearHistoryMenuItem;
}
